package object;

import java.awt.Color;

import util.ConnectionUtil;
import util.Settings;

import networkInterface.*;

public class Connection {
	private NetworkInterfaceBase[] networkInterfaces = new NetworkInterfaceBase[2];
	private double length = -1;
	
	public Color color = Settings.getInstance().getFrontColor();
	public Color defaultColor = Settings.getInstance().getFrontColor();
	
//	public Connection(NetworkInterfaceBase n1, NetworkInterfaceBase n2) throws NoConnectionPossibleException{
//		n1.addConnection(n2);
//	}
	
	public Connection(){
		
	}
	
//	public Connection(Node[] nodeArray){
//		if (nodeArray.length!=2)
//			throw new IllegalArgumentException("nodeArray must be of length 2");
//		nodeArray[0].addConnection(this);
//		nodeArray[1].addConnection(this);
//		networkInterfaces[0] = nodeArray[0];
//		networkInterfaces[1] = nodeArray[1];
//	}
	
	public NetworkInterfaceBase getOtherNetworkInterface(NetworkInterfaceBase netInt){
		if (netInt == networkInterfaces[0])
			return networkInterfaces[1];
		return networkInterfaces[0];
	}
//	public setOtherNode()
	
	public boolean isMember(Node n){
		if (n == networkInterfaces[0].getNode() || n == networkInterfaces[1].getNode())
			return true;
		return false; 
	}
	
	public boolean isMember(NetworkInterfaceBase i){
		if (i == networkInterfaces[0] || i == networkInterfaces[1])
			return true;
		return false; 
	}

//	public Node[] getNodes() {
//		return networkInterfaces;
//	}
	
	public int getN1X() {
		return networkInterfaces[0].getNode().getX();
	}
	public int getN1Y() {
		return networkInterfaces[0].getNode().getY();
	}
	public Node getN1() {
		return networkInterfaces[0].getNode();
	}

//	public void setN1(Node n1) throws InstantiationException{
//		//TODO: DELETE OLD CONNECTIONS IN NETWORKINTERFACE
//		if(this.networkInterfaces[0] == null || this.networkInterfaces[1] == null)
//			throw new InstantiationException("Cannot set one node at a time, unless both are already set");
//		this.networkInterfaces[0].setNode(n1);
//	}

	public int getN2X() {
		return networkInterfaces[1].getNode().getX();
	}
	public int getN2Y() {
		return networkInterfaces[1].getNode().getY();
	}
	public Node getN2() {
		return networkInterfaces[1].getNode();
	}

//	public void setN2(Node n2) throws InstantiationException{
//		//TODO: DELETE OLD CONNECTIONS IN NETWORKINTERFACE
//		if(this.networkInterfaces[0] == null || this.networkInterfaces[1] == null)
//			throw new InstantiationException("Cannot set one node at a time, unless both are already set");
//		this.networkInterfaces[1].setNode(n2);
//	}
	
	public double getLength(){
		if(length == -1){
			length = networkInterfaces[0].getNode().getLength(networkInterfaces[1].getNode());
			return length;
		} else {
			return length;
		}
	}
	
	/**
	 * Sets both interfaces
	 * <b>WARNING: Does not update the interfaces with connection. This object could be orphaned.
	 * @param i1 First NetworkInterface
	 * @param i2 Second NetworkInterface
	 */
	public void setInterfaces(NetworkInterfaceBase i1, NetworkInterfaceBase i2){
		networkInterfaces[0] = i1;
		networkInterfaces[1] = i2;
		if(i1 != null && i2 !=null){
			defaultColor = ConnectionUtil.getBetterConnectionColor(this);
			color = defaultColor;
		}
	}
	
	public void resetLength(){
		length = -1;
	}
	
	public NetworkInterfaceBase[] getInterfaces(){
		
		return networkInterfaces;
	}

	public Color getDefaultColor() {
		return defaultColor;
	}
	
//	public void removeConnection(){
//		
//	}
}
